package com.gisuni.sddc.platformManager.dao;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.gisuni.sddc.platformManager.model.LayerInfo;
import com.gisuni.sddc.platformManager.model.ResourceInfo;
import com.gisuni.sddc.platformManager.model.ResourceRLayer;
import com.gisuni.sddc.platformManager.model.ServerInfo;
import com.gisuni.sddc.util.UtilMethod;

@IocBean
public class DataThemeDao {
	
	@Inject
	private BasicDao basicDao;
 
	public List<ResourceInfo>  getAllTreeNode(){
		return basicDao.search(ResourceInfo.class, null);
	}
	public ResourceInfo addTreeNode(ResourceInfo resourceInfo){
		return basicDao.save(resourceInfo);
	}
	public List<ResourceInfo> findByPid(String id){
		Condition condition = Cnd.where("parentId", "=", id);
		return basicDao.search(ResourceInfo.class, condition);
	}
	public void deleteByIds(String id){
		ResourceInfo resourceInfo = basicDao.findByName(ResourceInfo.class, id);
		basicDao.getDao().clearLinks(resourceInfo, "roleInfos");
		basicDao.getDao().deleteWith(resourceInfo, "roleInfos");
	}

	public void editTreeNode(String id, String text) {
		Entity<ResourceInfo> entity =basicDao.getDao().getEntity(ResourceInfo.class);
		String table = entity.getTableName();
		Sql sql = Sqls.create("update " + table + " set resource_name ='"+text+"' where id='"+id+"'");
		basicDao.getDao().execute(sql);
	}
	
	public LayerInfo findLayerInfoByName(String id){
		return basicDao.findByName(LayerInfo.class, id);
	}
	
	public ServerInfo findServerInfoByName(String id){
		return basicDao.findByName(ServerInfo.class, id);
	}

	public boolean saveExpress(ResourceRLayer resourceRLayer) {
		//需要先查一下看是否存在
		ResourceRLayer rr = basicDao.findByName(ResourceRLayer.class, resourceRLayer.getResourceId());
		boolean flag=false;
		//如果存在了就更新一下，不存在在保存
		if(rr == null){
			resourceRLayer.setId(UUID.randomUUID().toString());
			basicDao.save(resourceRLayer);
			flag = true;
			return flag;
		}else{
			return basicDao.update(resourceRLayer);
		}
	}
	public ResourceRLayer getDefinedExpressById(String id) {
		return basicDao.findByName(ResourceRLayer.class, id);
	}
}
